
/**
 *
 * 工作流最顶层父类
 *
 * --------------
 *
 * API:
 *
 * addAttr(key, value, alias)
 * getAttr(key)
 * setAttr(key, value)
 * getAttrObjects()
 * transXMLNode2BPMObject(xmlNode)
 * parseAttr2Node()
 *
 */
(function(global){


    'use strict';

    const logger = WorkflowUtil.newLogger("WorkflowApp/BPMObject");


    // 内部类
    const Attribute = function (name, defaultValue, alias, notToXML) {

        // 属性名称
        this.name = name;

        // 名字别名(简写)
        this.alias = alias || name;

        // 属性值
        this.value = defaultValue;

        // 默认值
        this.defaultValue = defaultValue || "";

        // toXML时， 如果是默认值，过滤生成xml
        this.notToXML = notToXML === true;


        this.getName = function () {
            return this.name;
        };

        this.getAlias = function () {
            return this.alias;
        };

        this.setValue = function (value) {
            this.value = value;
        };

        this.getValue = function () {
            return this.value || this.defaultValue;
        };

        this.getDefaultValue = function(){
            return this.defaultValue;
        };

        this.isNotToXML = function(){
            return this.notToXML;
        }
    };


    /**
     * 构造器
     */
    let BPMObject = function () {

        //属性列表
        this.attrs = {};
        this.attrsKeys = [];
    };


    /**
     * 初始化属性
     * @param key 属性名称
     * @param value 属性默认值
     * @param alias 属性别名(简写)
     */
    BPMObject.prototype.addAttr = function(key, value, alias, notToXML){

        const attr = new Attribute(key, value, alias, notToXML);

        // 校验代码
        if(WorkflowUtil.isDebug()){
            // 调试代码
            const oldAttr = this.attrs[key];
            if(oldAttr){
                logger.error("存在重复的Key：" + key + ", 对象类型:" + this.getBPMObjectType());
            }else{
                // 校验简称有没有问题
                for(let akey in this.attrs){
                    if(this.attrs[akey].getAlias() === alias){

                        let msgInfo = "\n存在重复的简称 alias: " + alias + ", 对象类型:" + this.getBPMObjectType();
                        msgInfo += "\n原来的属性key：" + this.attrs[akey].getName();
                        msgInfo += "\n新增的属性key：" + key;
                        logger.error(msgInfo);
                    }
                }
            }
        }

        this.attrsKeys.push(key);
        this.attrs[key] = attr;
    };

    /**
     * 获取属性值
     * @param key
     * @returns
     */
    BPMObject.prototype.getAttr = function(key){
        const attr = this.attrs[key];
        if(attr){
            return attr.getValue();
        }
        logger.warn("获取不存在的属性: " + key + ", 对象类型:" + this.getBPMObjectType());
        return "";
    };
    
    BPMObject.prototype.getBPMObjectType = function(){
        return "undefined BPMObjectType";
    };

    /**
     * 设置属性值
     * @param key
     * @param value
     */
    BPMObject.prototype.setAttr = function(key, value){

        const attr = this.attrs[key];
        if(attr){
            return attr.getValue();
        }
        logger.warn("设置不存在的属性: key='" + key + "', value='" + value + "'" + ", 对象类型:" + this.getBPMObjectType());
    };
    
    /**
     * 重置所有属性
     */
    BPMObject.prototype.resetAllAtt = function(){
        
        for(let key of this.attrsKeys){
            this.attrs[key].setValue(null);
        }
    };

    /**
     * 获取全部的属性对象
     */
    BPMObject.prototype.getAttrObjects = function(){
        const attrs = [];
        for(let key of this.attrsKeys){
            attrs.push(this.attrs[key]);
        }
        return attrs;
    };

    BPMObject.prototype.getAllAttrsKeys = function(){
        return [...this.attrsKeys];
    };


    /**
     * 获取属性的xml格式
     * @returns {string}
     */
    BPMObject.prototype.parseAttr2Node = function(xmlNode){

        for(let key of this.attrsKeys){

            let attr = this.attrs[key];

            if(attr.isNotToXML() && attr.getValue() === attr.getDefaultValue()){
                // 属性值等于默认值， 不转换成xml
            }else{
                xmlNode.setAttribute(attr.getAlias(), attr.getValue());
            }
        }
    };

    
    /**
     * 解析xmlNode到BPMObject中
     * @param xmlNode
     */
    BPMObject.prototype.transXMLNode2BPMObject = function(xmlNode){

        const attrObjs = this.getAttrObjects();
        let i = 0, attrObjsLength = attrObjs.length;
        for(; i < attrObjsLength; i++){

            let attrObj, alias, xmlAttrValue;

            attrObj = attrObjs[i];
            alias = attrObj.getAlias();
            xmlAttrValue = xmlNode.getAttribute(alias);
            attrObj.setValue(xmlAttrValue);
        }
    };
    
    global.BPMObject = BPMObject;

})(WorkflowApp);
